package com.wrong.desgin.applicaiton.violate_design;

import com.wrong.desgin.RedisUtils;
import com.wrong.desgin.config.RedisCluster;
import com.wrong.desgin.redis.cluster.EGM;
import com.wrong.desgin.redis.cluster.IIR;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author: hliushi
 * @date: 2022/12/5 23:22
 **/
public class WrongCacheServiceImpl implements WrongCacheService {

    /**
     * 原本是使用单体的 redis, 需要兼容升级到 Redis 集群中
     */
    private RedisUtils redisUtils = new RedisUtils();

    /**
     * 后面新添加的两个 redis 集群, 通过快速添加 if else 已经扩充方法的参数
     */
    private IIR iir = new IIR();
    private EGM egm = new EGM();

    @Override
    public Object get(String key, String type) {
        if (Objects.equals(type, RedisCluster.EGM.name())) {
            return egm.gain(key);
        }

        if (Objects.equals(type, RedisCluster.IIR.name())) {
            return iir.get(key);
        }
        return redisUtils.get(key);
    }

    @Override
    public void set(String key, Object value, String type) {
        if (Objects.equals(type, RedisCluster.EGM.name())) {
            egm.set(key, value);
            return;
        }

        if (Objects.equals(type, RedisCluster.IIR.name())) {
            iir.set(key, value);
            return;
        }
        redisUtils.set(key, value);
    }

    @Override
    public void set(String key, Object value, long timeout, TimeUnit unit, String type) {
        if (Objects.equals(type, RedisCluster.EGM.name())) {
            egm.setEx(key, value, timeout, unit);
            return;
        }

        if (Objects.equals(type, RedisCluster.IIR.name())) {
            iir.setExpire(key, value, timeout, unit);
            return;
        }
        redisUtils.set(key, value, timeout, unit);
    }

    @Override
    public void del(String key, String type) {
        if (Objects.equals(type, RedisCluster.EGM.name())) {
            egm.delete(key);
            return;
        }

        if (Objects.equals(type, RedisCluster.IIR.name())) {
            iir.del(key);
            return;
        }
        redisUtils.del(key);
    }
}
